<template>
	<view class="content" :style="{ height: windowHeight + 'px' }">
		<Tabbar :tabbar-index="tabbarIndex" @updateTabbar="updateTabbar"></Tabbar>

		<view class="heaedr">
			<image class="back"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776946192_3decace2/back.png?x-oss-process=image/resize,w_300"
				alt=""></image>
			<image class="pk"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777427633_11d5fe0f/pk.png?x-oss-process=image/resize,w_300"
				alt="" @click="pkClick"></image>
			<image class="lianxi"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777287778_7654460e/lianxi.png?x-oss-process=image/resize,w_300"
				alt="" @click="lianxiClick"></image>
			<image class="shengji"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/6/17/1750148749623_dc49c6a2/shengji%5B1%5D.png?x-oss-process=image/resize,w_300"
				alt="" @click="shengjiClick"></image>
			<view class="contnet">
				<image class="touxiang" :src="avatarImage" alt=""></image>
				<view class="contents">
					<view class="name">{{ userName }}</view>
					<view class="miaoshu">
						<view class="dnegji">启萌{{ indesActive }}级</view>
					</view>
				</view>
			</view>
			<image class="shezhi"
				src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777532027_72e1b1b8/shezhi.png?x-oss-process=image/resize,w_300"
				alt="" @click="shezhiClick"></image>
		</view>

		<view class="nav">
			<view class="nav_items" v-for="(ite, index) in navList" :key="index" @click="navClcik(ite.link)">
				<image class="image" :src="ite.img" alt=""></image>
				<view class="label">{{ ite.label }}</view>
			</view>
		</view>

		<!-- 报名竞赛 -->
		<!-- <image class="xuanchuan" :src="competitionData.titleImage" alt="" v-if="Object.keys(competitionData).length">
		</image> -->

		<image class="xuanchuan" :src="competitionStartImage" alt=""></image>

		<view class="jingsai" v-if="Object.keys(competitionData).length">
			<view class="header">
				<image
					src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777287779_13764840/jingsai.png?x-oss-process=image/resize,w_300"
					mode=""></image>
				竞赛考试
			</view>
			<view class="jingsai_content">
				<view class="jingsai_left">
					<view class="names">{{ competitionData.title }}</view>
					<view class="times">{{ competitionData.des }}</view>
				</view>
				<view class="jingsai_rights" v-if="competitionData.des == '竞赛已经开始啦'" @click="begineXamination">开始考试</view>
				<view class="jingsai_right" v-else >等待考试</view>
			</view>
		</view>

		<view class="bottom">
			<view class="bottom_item" v-for="(item, index) in bottomList" :key="index" @click="navClick(item)">
				<image class="image" :src="item.img" alt=""></image>
				<view class="label">{{ item.label }}</view>
			</view>
		</view>

		<view class="copyright">
			<view class="">
				Copyright@2025
			</view>
			<view class="">
				石河子市独角鲸信息科技有限公司 版权所有
			</view>
		</view>

		<!-- 等级测试 -->
		<u-popup :show="practiceShow" mode="center" :round="10">
			<view class="practice_pop">
				<view class="clsoe">
					<image
						src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776289685_6c0d3527/close.png?x-oss-process=image/resize,w_300"
						alt="" @click="practiceShow = false"></image>
				</view>
				<view class="title">
					等级测试
				</view>
				<view class="grade">
					{{ practiceData.levelId }}
				</view>
				<view class="des">
					当前等级为
					{{ practiceData.name }}，加油，加油，加油
				</view>
				<view class="des">
					达到{{ practiceData.upgradeNum }}分，即可升级
				</view>
				<view class="button" @click="upgrade">
					开始测试
				</view>
			</view>
		</u-popup>

		<!-- 每日一练 -->
		<u-popup :show="everyShow" mode="center" :round="30">
			<view class="practice_pop">
				<view class="clsoe">
					<image
						src="https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776289685_6c0d3527/close.png?x-oss-process=image/resize,w_300"
						alt="" @click="everyShow = false"></image>
				</view>
				<view class="title">
					每日一练
				</view>
				<view class="image">
					<image :src="item ? item : ''" v-for="(item, index) in avatarImageList" :key="index" alt="">
					</image>
				</view>
				<view class="des">
					已有{{ totalFinish }}次练习啦，快来一起练习吧
				</view>
				<view class="button" @click="practice">
					开始练习
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import Tabbar from "@/components/tabbar/tabbar.vue"
import {
	systemInfo
} from '../../../utils/mixin'
import {
	getPracticeNum,
	getInfo,
	checkTestLevel,
	competitionnotStart,
	hasQuestion,
	hasUpgradeQuestion,
	cancel,
	getImageConfig
} from '@/api/index.js'
import config from '@/config/config.js'
export default {
	components: {
		Tabbar
	},
	mixins: [systemInfo],
	data() {
		return {
			tabbarIndex: 4,
			// 每日练习-等级测试
			practiceShow: false,
			// 每日一练
			everyShow: false,
			// 总题数
			questionNum: 0,
			// 答对数量
			correctNum: 0,
			// 升级数量
			upgradeNum: 0,
			// 总练习数量
			totalFinish: 0,
			// 每日一练头像
			avatarImageList: [],
			// 等级测试
			practiceData: {},
			navList: [{
				label: '积分商城',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777532026_46c1de3b/tuihuo.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/shop/shop'
			},
			{
				label: '我的评价',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777427632_7a451802/pingjia.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/pingjia/pingjia'
			},
			{
				label: '我的订单',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776946196_2371029e/dingdan.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/order/order'
			},
			],
			bottomList: [{
				label: '会员中心',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777151825_abe8988f/huiyaun.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/member/member'
			},
			{
				label: '我的积分',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777151829_40b1eb68/jifen.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/points/points'
			},
			{
				label: '我的收藏',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777532027_f80847ef/shoucang.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/collect/collect'
			},
			{
				label: '我的等级',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776946195_d383530c/dengji.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/grade/grade'
			},
			{
				label: '我的圈子',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777427633_b83c2790/quanzi.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/circle/circle'
			},
			{
				label: '我的纠错',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777287776_d05fda27/jiucuo.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/correction/correction'
			},
			{
				label: '我的题库',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777532028_223a5769/tiku.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/questionBank/questionBank'
			},
			{
				label: '我的竞赛',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777287779_11bdb513/jignsai.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/competition/competition'
			},
			{
				label: '收货地址',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777151829_96f6e81b/dizhi.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/address/address'
			},
			{
				label: '我的学情',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777705564_8d77f47f/xueqing.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/study/study'
			},
			{
				label: '我的勋章',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753776946195_e055d076/dianliang.png?x-oss-process=image/resize,w_300',
				link: '/subpackage3/light/light'
			},
			{
				label: '注销',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777705566_ccaf9b69/zhuxiao.png?x-oss-process=image/resize,w_300',
				link: ''
			},
			{
				label: '隐私政策',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777705566_ffe4a36f/yinsizhengce.png?x-oss-process=image/resize,w_300',
				link: '/subpackage1/login/policy'
			},
			{
				label: '退出账户',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753777532023_63724f67/tuichu.png?x-oss-process=image/resize,w_300'
			},
			{
				label: '举报投诉',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753757111688_a698bc21/KHCFDC_%E5%AE%A2%E6%88%B7%E6%8A%95%E8%AF%89%5B1%5D.png?x-oss-process=image/resize,w_300',
				link: '/subpackage4/feedback/feedback'
			},
			{
				label: '客户服务',
				img: 'https://dujiaojing.oss-cn-huhehaote.aliyuncs.com/uploads/2025/7/29/1753757123004_caf73fe6/%E7%94%B5_%E8%AF%9D01%5B1%5D.png?x-oss-process=image/resize,w_300',
				link: '/subpackage4/parameter/parameter'
			},
			],
			userName: '',
			indesActive: 0,
			avatarImage: '',
			// 竞赛
			competitionData: {},


			competitionStartImage: ''
		};
	},
	onLoad() {
		// 状态栏高度
		this.getSystemInfo();

		// #ifdef APP-PLUS || H5
		uni.hideTabBar();
		// #endif
	},
	onShow() {
		this.userName = uni.getStorageSync('userName')
		this.getDetail()
		this.getcompetitionnotStart()
		// 获取竞赛首页图片
		getImageConfig().then(res => {
			this.competitionStartImage = config.pic_prefixs + res.data.competitionStartImage
		})
	},
	methods: {
		// 获取竞赛
		async getcompetitionnotStart() {
			const res = await competitionnotStart()
			if (res.rows.length > 0) {
				this.competitionData = res.rows[0]
				this.competitionData.titleImage = config.pic_prefixs + this.competitionData.titleImage
				this.competitionData.startTime = this.competitionData.startTime.split(' ')[0]
				const now = new Date().getTime()
				if ((new Date() >= new Date(res.rows[0].startTime)) && (new Date() <= new Date(res.rows[0].endTime))) {
					this.competitionData.des = '竞赛已经开始啦'
				} else {
					const timeDiff = Math.abs(now - new Date(this.competitionData.startTime).getTime());
					// 计算天数差值
					const daysDiff = Math.floor(timeDiff / (24 * 60 * 60 * 1000));
					this.competitionData.des = '还有' + daysDiff + '天开始考试啦'
				}
			} else {
				this.competitionData = {}
			}
		},
		// 获取详细信息
		async getDetail() {
			const res = await getInfo()
			this.indesActive = res.data.user.levelId
			this.avatarImage = config.pic_prefix + res.data.user.avatarImage
			uni.setStorageSync('userName', res.data.user.nickName);
			uni.setStorageSync('deptName', res.data.user.deptName);
			if (res.data.user.levelId == 0) {
				const resp = await checkTestLevel()
				this.practiceData = resp.data
			} else {
				// 练习数量
				this.getgetPracticeNum();
			}
		},
		// 练习
		async lianxiClick() {
			if (this.indesActive == 0) {
				this.practiceShow = true
			} else {
				if (this.questionNum == 0) {
					uni.$u.toast('暂无练习题！');
				} else {
					this.everyShow = true
				}
			}
		},
		// 升级
		async shengjiClick() {
			const res = await hasUpgradeQuestion()
			if (res.code == 200) {
				uni.navigateTo({
					url: "/subpackage3/shengji/shengji"
				})
			}
		},
		// pk
		async pkClick() {
			uni.navigateTo({
				url: "/subpackage3/pk/pk"
			})
		},
		// 升级
		upgrade() {
			uni.navigateTo({
				url: "/subpackage2/upgrade/upgrade"
			})
			this.practiceShow = false
		},

		// 获取所有每日练模块数量
		async getgetPracticeNum() {
			const res = await getPracticeNum()
			this.questionNum = res.data.questionNum
			this.correctNum = res.data.correctNum
			this.upgradeNum = Math.floor(this.correctNum / res.data.upgradeNum * 100)
			this.totalFinish = res.data.totalFinish
			this.avatarImageList = []
			if (res.data.avatarImageList) {
				res.data.avatarImageList.forEach(item => {
					this.avatarImageList.push(config.pic_prefix + item)
				})
			}
		},
		// 开始练习
		async practice() {
			this.everyShow = false
			const res = await hasQuestion()
			if (res.code == 200) {
				uni.navigateTo({
					url: "/subpackage2/practice/practice"
				})
			} else {
				// uni.$u.toast(res.msg)
			}
		},

		updateTabbar(e) {
			this.tabbarIndex = e
		},
		// 设置
		shezhiClick() {
			uni.navigateTo({
				url: "/subpackage3/person/person"
			})
		},
		// 我的模块跳转
		async navClick(item) {
			if (item.link) {
				uni.navigateTo({
					url: item.link
				})
			} else if (item.label == '退出账户') {
				uni.showModal({
					title: '是否退出登录',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('token')
							uni.removeStorageSync('userName')
							uni.removeStorageSync('deptName')
							uni.switchTab({
								url: '/pages/tabbar/home/home'
							})
						}
					}
				})
			} else if (item.label == '注销') {
				const response = await cancel()
				if (response.code == 200) {
					uni.$u.toast('注销成功！');
					uni.removeStorageSync('token')
					uni.removeStorageSync('userName')
					uni.removeStorageSync('deptName')
				}
			}
		},
		// 开始考试
		begineXamination() {
			uni.navigateTo({
				url: "/subpackage2/competitionExam/competitionExam?id=" + this.competitionData.id
			})
		},
		// 我的点亮
		gotos() {
			uni.navigateTo({
				url: "/subpackage3/light/light"
			})
		},
		// 商城
		navClcik(link) {
			console.log(link)
			uni.navigateTo({
				url: link
			})
		}
	}
};
</script>

<style lang="less" scoped>
.content {
	width: 100%;
	box-sizing: border-box;
	padding-bottom: 132rpx;
	overflow-y: auto;

	.heaedr {
		width: 100%;
		height: 490rpx;
		position: relative;

		.shezhi {
			position: absolute;
			top: 190rpx;
			right: 60rpx;
			width: 48rpx;
			height: 46rpx;
		}

		.contnet {
			position: absolute;
			top: 160rpx;
			left: 60rpx;
			display: flex;
			align-items: center;

			.touxiang {
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
			}

			.contents {
				width: 180rpx;
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				margin-left: 20rpx;
				height: 90rpx;
				align-content: space-between;

				.name {
					color: #fff;
					font-size: 36rpx;
					width: 100%;
				}

				.miaoshu {
					display: flex;
					align-items: center;
					font-size: 20rpx;
					color: #fff;
					width: 100%;
					justify-content: space-between;

					.image {
						width: 20rpx;
						height: 20rpx;
					}
				}
			}
		}

		.back {
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
		}

		.pk {
			width: 140rpx;
			height: 140rpx;
			position: absolute;
			left: 70rpx;
			top: 380rpx;
		}

		.lianxi {
			width: 140rpx;
			height: 140rpx;
			position: absolute;
			left: 300rpx;
			top: 380rpx;
		}

		.shengji {
			width: 140rpx;
			height: 140rpx;
			position: absolute;
			right: 70rpx;
			top: 380rpx;
		}

	}

	.nav {
		margin-top: 60rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20rpx;

		.nav_items {
			width: calc(100% / 3);
			text-align: center;

			.image {
				width: 44rpx;
				height: 48rpx;
				margin-bottom: 10rpx;
			}

			.label {
				font-size: 30rpx;
			}
		}
	}

	.xuanchuan {
		width: calc(100% - 40rpx);
		height: 130rpx;
		margin-top: 40rpx;
		margin-left: 20rpx;
		border-radius: 5px;
	}

	.jingsai {
		margin-top: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20rpx;
		flex-wrap: wrap;

		.header {
			width: 100%;
			color: rgb(179, 173, 173);
			font-size: 26rpx;
			display: flex;
			align-items: center;

			image {
				width: 26rpx;
				height: 26rpx;
				margin-right: 10rpx;
			}
		}

		.jingsai_content {
			width: 100%;
			box-sizing: border-box;
			padding-left: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;

			.jingsai_left {
				.names {
					font-size: 40rpx;
					margin-bottom: 10rpx;
				}

				.times {
					color: rgb(179, 173, 173);
					font-size: 22rpx;
				}
			}

			.jingsai_right {
				width: 150rpx;
				height: 48rpx;
				background-color: rgb(223, 223, 223);
				color: rgb(179, 173, 173);
				border-radius: 10rpx;
				font-size: 24rpx;
				text-align: center;
				line-height: 48rpx;
			}

			.jingsai_rights {
				width: 150rpx;
				height: 48rpx;
				background-color: rgb(34, 175, 229);
				color: #fff;
				border-radius: 10rpx;
				font-size: 24rpx;
				text-align: center;
				line-height: 48rpx;
			}
		}
	}


	.bottom {
		margin-top: 60rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 0 20rpx;
		flex-wrap: wrap;

		.bottom_item {
			width: calc(100% / 4);
			text-align: center;
			margin-bottom: 30rpx;

			.image {
				width: 42rpx;
				height: 42rpx;
				margin-bottom: 10rpx;
			}

			.label {
				font-size: 30rpx;
			}
		}
	}

	.copyright {
		text-align: center;
		width: 100%;
		margin-top: 120rpx;
		color: rgb(179, 173, 173);
		font-size: 30rpx;
		margin-bottom: 100rpx;
	}

	.practice_pop {
		width: 670rpx;
		height: 670rpx;
		box-sizing: border-box;
		padding: 20rpx;

		.clsoe {
			width: 100%;
			height: 75rpx;
			text-align: right;

			image {
				width: 48rpx;
				height: 48rpx;
				opacity: 0.5;
			}
		}

		.title {
			width: 100%;
			text-align: center;
			font-size: 40rpx;
			font-weight: 500;
			letter-spacing: 3rpx;
		}

		.image {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 100rpx 0;

			image {
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
			}
		}

		.grade {
			font-size: 200rpx;
			font-weight: 800;
			text-align: center;
			width: 100%;
			color: rgb(254, 0, 0);
		}

		.des {
			width: 100%;
			text-align: center;
			font-size: 26rpx;
			font-weight: 500;
			letter-spacing: 3rpx;
			line-height: 2;
			color: rgb(179, 173, 173);
		}

		.button {
			margin: 30rpx auto;
			width: 280rpx;
			height: 76rpx;
			text-align: center;
			line-height: 76rpx;
			border-radius: 10rpx;
			color: #fff;
			font-size: 28rpx;
			background-color: rgb(64, 149, 229);
			letter-spacing: 3rpx;
		}
	}
}
</style>